package test4;

public class StudentsTest {
    public static void main(String[] args) {
        Students arr[] = new Students[3];
        Students s1 = new Students(1, "小灵", 17);
        Students s2 = new Students(2, "小雅", 18);
        arr[0] = s1;
        arr[1] = s2;
        Students s3 = new Students(3, "小婷", 19);
        boolean flagg = flag(arr, s3.getId());
        if (flagg) {
            System.out.println("当前id重复，请修改id后再进行添加");
        } else {
            int count = count(arr);
            Students arr2[]=zy(arr,count);
            arr2[count]=s3;
            for (int i = 0; i < arr2.length; i++) {
                if(arr2[i]!=null) {
                    System.out.println(arr2[i].getId() + arr2[i].getName() + arr2[i].getAge());
                }
            }
        }
    }

    //判断数组中有几个元素
    public static int count(Students arr[]) {
        int countt = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                countt++;
            }
        }
        return countt;
    }

    //判断数组满
    public static Students[] zy(Students arr[], int count) {
        if (arr.length == count + 1) {
     Students newarr[]= zyy(arr);
        return newarr;
        }
        else {
            return arr;
        }
    }

    //创建新数组
    public static Students[] zyy(Students arr[]) {
        Students arr1[] = new Students[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            arr1[i]=arr[i];
        }
               return arr1;
    }


    public static boolean flag(Students arr[], int id) {
        for (int i = 0; i < arr.length; i++) {
            Students stu = new Students();
            stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
        }
        return false;
    }
}


